package com.assignwork.modules.pc.service.pc.impl;
import com.assignwork.dto.pcplan.PcShipInfoEnteringSearchDto;
import com.assignwork.dto.pcplan.PcShipInfoEnteringSubmitDto;
import com.assignwork.entity.pc.PcShipInfoEntering;
import com.assignwork.entity.pc.PcShipPlan;
import com.assignwork.mapper.PcShipInfoEnteringMapper;
import com.assignwork.modules.pc.service.pc.IPcShipInfoEnteringService;
import com.assignwork.vo.pc.PcShipInfoEnteringVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName PcShipInfoEnteringServiceImpl
 * @Description
 * @Author sl
 * @Date 2023/10/16 10:18
 */
@Service
@AllArgsConstructor
public class PcShipInfoEnteringServiceImpl extends BaseServiceImpl<PcShipInfoEnteringMapper, PcShipInfoEntering> implements IPcShipInfoEnteringService {
    private PcShipPlanServiceImpl pcShipPlanService;
    @Override
    public IPage<PcShipInfoEnteringVo> selectPcShipInfoEnteringPage(PcShipInfoEnteringSearchDto dto, IPage<PcShipInfoEnteringVo> page) {
        List<PcShipInfoEnteringVo> vos = baseMapper.selectPcShipInfoEnteringPage(dto, page);
        vos.forEach(vo -> {
            if (!Func.isNull(vo.getWorkCompanyKey()) && !Func.isBlank(vo.getWorkCompanyKey())) {
                vo.setWorkCompanyValue(DictBizCache.getValue("work_company", vo.getWorkCompanyKey()));
            }
            if (!Func.isNull(vo.getIe()) && !Func.isBlank(vo.getIe())) {
                vo.setIeValue(DictBizCache.getValue("ie_type", vo.getIe()));
            }
            if (!Func.isNull(vo.getCargoTypePc()) && !Func.isBlank(vo.getCargoTypePc())) {
                vo.setCargoTypePcValue(DictBizCache.getValue("pc_cargo_type", vo.getCargoTypePc()));
            }
        });
        return page.setRecords(vos);
    }

    @Override
    public boolean submitPcShipInfoEntering(PcShipInfoEnteringSubmitDto submitDTO) {
        PcShipInfoEntering copy = BeanUtil.copy(submitDTO, PcShipInfoEntering.class);
        return saveOrUpdate(copy);
    }

    @Override
    public List<PcShipInfoEnteringVo> selectPcShipInfoEnteringList(PcShipInfoEnteringSearchDto dto) {
        List<PcShipInfoEnteringVo> ShipInfoEnteringListIsCompany = baseMapper.selectPcShipInfoEnteringListIsCompany(dto);
        ShipInfoEnteringListIsCompany.forEach(vo -> {
            if (!Func.isNull(vo.getWorkCompanyKey()) && !Func.isBlank(vo.getWorkCompanyKey())) {
                vo.setWorkCompanyValue(DictBizCache.getValue("work_company", vo.getWorkCompanyKey()));
            }
            if (!Func.isNull(vo.getIe()) && !Func.isBlank(vo.getIe())) {
                vo.setIeValue(DictBizCache.getValue("ie_type", vo.getIe()));
            }
            if (!Func.isNull(vo.getCargoTypePc()) && !Func.isBlank(vo.getCargoTypePc())) {
                vo.setCargoTypePcValue(DictBizCache.getValue("pc_cargo_type", vo.getCargoTypePc()));
            }
        });
        return ShipInfoEnteringListIsCompany;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removePcShipInfoEntering(String id) {
        List<PcShipPlan> pcShipPlanList = pcShipPlanService.lambdaQuery().eq(PcShipPlan::getShipId, Long.valueOf(id)).list();
        pcShipPlanList.forEach(e->{
            pcShipPlanService.removeById(e.getId());
        });
        deleteLogic(Func.toLongList(id));

        return true;
    }
}
